'use strict';
mui.init();
var auths = {}//第三方登录认证

mui.plusReady(function () {
	/*var height1 = document.documentElement.clientHeight || document.body.clientHeight;
	//窗口大小改变时候 解决手机键盘导致布局bug
	window.onresize = function () {
		var height2 = document.documentElement.clientHeight || document.body.clientHeight;
		if (height1 > height2) { 
			mui('#footer')[0].style.display = 'none';
		} else {
			mui('#footer')[0].style.display = 'block';
		}
	}*/

	//登录验证
	document.getElementById('login').addEventListener('tap', function () {
		var reg = /^1(([358]\d)|(47)|7[0178])\d{8}$/;
		var usernameInput = document.querySelector('input[name="username"]');
		var passwordInput = document.querySelector('input[name="password"]');
		var usernameValue = usernameInput.value;
		var passwordValue = passwordInput.value;
		if (!usernameValue || !passwordValue) {
			mui.toast('用户名或密码不能为空!');
		} else if (!usernameValue.match(reg)) {
			mui.toast('是否是有效手机号!');
		} else {
			/*mui.ajax({
				url: url1, //login 接口请求
				type: 'post',
				data: {
					'username': usernameValue,
					'password': passwordValue
				},
				headers: {
					"X-LC-Id": "Us4gQNj1kdx1Cv3oi06hLnGu-gzGzoHsz",
					"X-LC-Key": "27exTSjI41ozaFxmL3XDIkNn"
				},
				success: function (response) {
					mui.toast('登录成功!');
					localStorage.setItem('sessionToken', response.sessionToken);
					localStorage.setItem('username', response.username);
					setTimeout(function () {
						mui.openWindow('../main/main.html', 'main');				
					}, 2000);
				},
				error: function (error) {
					mui.toast('账号或者密码错误!')
				}
			})*/
			//测试
			if (usernameValue === '15857009524' && passwordValue === '123456') {
				mui.openWindow('../main/main.html', 'main', 'fade-in');
			} else {
				mui.toast('用户名或密码错误,请重新输入!')
			}
		}
		
		
	});
	
	//显示密码
	mui('.toggle')[0].addEventListener('tap', function (e) {
		if (this.innerHTML === '显示') {
			this.innerHTML = '隐藏';
			mui('.point')[0].style.display = 'block';
			mui('input[name="password"]')[0].setAttribute('type', 'hidden');
		} else {
			this.innerHTML = '显示';
			mui('.point')[0].style.display = 'none';
			mui('input[name="password"]')[0].setAttribute('type', 'text');
		}
	})
			
	//忘记密码
	//获取当前的webview	
	var currentWv = plus.webview.currentWebview();
	mui('#go1')[0].addEventListener('tap', function () {
		var wv1 = plus.webview.create('forget.html', 'fotget', {})
		wv1.show('pop-in');//显示创建的webview
	})
	
	//立即注册
	mui('#go2')[0].addEventListener('tap', function () {
		var wv2 = plus.webview.create('register.html', 'register', {})
		wv2.show('pop-in');//显示创建的webview
	})	
	
	//第三方登录
	mui('#footer')[0].addEventListener('tap', 'a', function (e) {
		mui.toast('a')
	})	
	
	//oauth第三登录认证模块
	plus.oauth.getServices(function(services){
		console.log(services)
		var txt="登录认证通道信息：";
		for(var i in services){
			var service=services[i];
			console.dir(service)
			console.log(service.id+": "+service.authResult+", "+service.userInfo);
			auths[service.id]=service;
			txt += "id:"+service.id+", ";
			txt += "description:"+service.description+", ";
		}
		console.log(txt);
	},function(e){
		outLine("获取登录认证失败："+e.message);
	});
	
})


// 登录认证
function login(id){
	outSet("----- 登录认证 -----");
	var auth=auths[id];
	if(auth){
		var w=null;
		if(plus.os.name=="Android"){
			w=plus.nativeUI.showWaiting();
		}
		document.addEventListener("pause",function(){
			setTimeout(function(){
				w&&w.close();w=null;
			},2000);
		}, false );
		auth.login(function(){
			w&&w.close();w=null;
			outLine("登录认证成功：");
			outLine(JSON.stringify(auth.authResult));
			userinfo(auth);
		},function(e){
			w&&w.close();w=null;
			outLine("登录认证失败：");
			outLine("["+e.code+"]："+e.message);
			plus.nativeUI.alert("详情错误信息请参考授权登录(OAuth)规范文档：http://www.html5plus.org/#specification#/specification/OAuth.html",null,"登录失败["+e.code+"]："+e.message);
		});
	}else{
		outLine("无效的登录认证通道！");
		plus.nativeUI.alert("无效的登录认证通道！",null,"登录");
	}
}

// 获取用户信息
function userinfo(a){
	outLine("----- 获取用户信息 -----");
	a.getUserInfo(function(){
		outLine("获取用户信息成功：");
		outLine(JSON.stringify(a.userInfo));
		var nickname=a.userInfo.nickname||a.userInfo.name||a.userInfo.miliaoNick;
		plus.nativeUI.alert("欢迎“"+nickname+"”登录！");
	},function(e){
		outLine("获取用户信息失败：");
		outLine("["+e.code+"]："+e.message);
		plus.nativeUI.alert("获取用户信息失败！",null,"登录");
	});
}
	
// 注销登录
function logoutAll(){
	outSet("----- 注销登录认证 -----");
	for(var i in auths){
		logout(auths[i]);
	}
}
function logout(auth){
	auth.logout(function(){
		outLine("注销\""+auth.description+"\"成功");
	},function(e){
		outLine("注销\""+auth.description+"\"失败："+e.message);
	});
}
	
	